package com.fragment;

import java.util.ArrayList;
import java.util.List;

import model.LTModel;
import model.LTServiceModel;
import model.PeopleModel;

import org.json.JSONObject;
import org.xutils.DbManager;
import org.xutils.x;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import com.dialog.AlertDialog;
import com.friehome.app.Main2Activity;
import com.friehome.app.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.net.HttpRequestUtil;
import com.net.HttpResponse;
import com.net.TTHttpRequestControll;
import com.utils.TTUtils;

import adapter.IndexAdapter;
import adapter.MainAdapter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class Fragment1 extends BaseFragment{

	@ViewInject(R.id.text1)
	TextView text1;
	@ViewInject(R.id.text2)
	TextView text2;
	@ViewInject(R.id.text3)
	TextView text3;
	@ViewInject(R.id.text4)
	TextView text4;
	@ViewInject(R.id.text5)
	TextView text5;
	@ViewInject(R.id.text6)
	TextView text6;
	@ViewInject(R.id.text7)
	TextView text7;
	@ViewInject(R.id.text8)
	TextView text8;
	@ViewInject(R.id.text9)
	TextView text9;
	
	@ViewInject(R.id.btn_main2)
	TextView btn_main2;
	@ViewInject(R.id.btn_main3)
	TextView btn_main3;
	
	@ViewInject(R.id.listView)
	ListView listView;
	
	@ViewInject(R.id.listView2)
	ListView listView2;
	
	List<LTServiceModel> data1 = new ArrayList<LTServiceModel>();
	List<LTServiceModel> data2 = new ArrayList<LTServiceModel>();
	
	PeopleModel peopleModel;
	String id;
	String status;
	String taskId;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment1, null);
		x.view().inject(this, view);
		
		id = getArguments().getString("id");
		
		
		
		
		getData();
		
		
		
		
		return view;
	}
	
	public void setView(){
		text1.setText(peopleModel.name);
		text2.setText(peopleModel.sex);
		text3.setText(peopleModel.address);
		text4.setText(peopleModel.lianxiren);
		text5.setText(peopleModel.lianxidianhua);
		text6.setText(peopleModel.timeBegin);
		text7.setText(peopleModel.timeEnd);
		text8.setText(peopleModel.remark);
		text9.setText(peopleModel.task);
		
	}
	
	@Event(value={R.id.btn_main2,R.id.btn_main3})
	private void onClick(View v){
		
		if (TextUtils.isEmpty(taskId)) {
			toast("无逝者数据");
			return;
		}
		switch (v.getId()) {
		
		case R.id.btn_main2:
			status = "begin";
			showAlertDialog("确定要操作“开始告别”？");
			break;
		case R.id.btn_main3:
			status = "end";
			showAlertDialog("确定要操作“完成告别”？");
			break;

		default:
			break;
		}
	}
	
	public void getData(){
		TTHttpRequestControll.httpMain1(getActivity(),id ,new HttpRequestUtil.HttpRonspontCallBack() {
			
			@Override
			public void callBack(HttpResponse result) {
				
				try {
					if (result.success) {
						JSONObject ob=  new JSONObject(result.content);
						
						
						((Main2Activity)getActivity()).dc = ob.getString("daoci");
						((Main2Activity)getActivity()).taskId = ob.getString("taskId");
						taskId = ob.getString("taskId");
						peopleModel = new Gson().fromJson(ob.getJSONObject("deadInfo").toString(), PeopleModel.class);
						Log.e("data", "" + new Gson().toJson(peopleModel));
						((Main2Activity)getActivity()).deadId = peopleModel.id;
						data1 = new Gson().fromJson(ob.getJSONArray("serviceLiting").toString(), new  TypeToken<List<LTServiceModel>>(){}.getType() );
						data2 = new Gson().fromJson(ob.getJSONArray("serviceMode").toString(), new  TypeToken<List<LTServiceModel>>(){}.getType() );

						listView.setAdapter(new MainAdapter(getActivity(), data1));
						listView2.setAdapter(new MainAdapter(getActivity(), data2));
						
						setView();
						if (ob.getInt("ServiceStatus") == 0) {
							btn_main2.setClickable(true);
							btn_main3.setClickable(false);
						}else if (ob.getInt("ServiceStatus") == 1) {
							btn_main2.setClickable(false);
							btn_main3.setClickable(true);
						}
					}else{
						toast(result.errorMsg);
					}
				} catch (Exception e) {
					e.printStackTrace();
					toast("目前无告别任务");
				}
				
					
				
			}
		});
	}
	
	public void showAlertDialog(String msg){
		AlertDialog dilog=  new AlertDialog(getActivity(), msg, new AlertDialog.DialogClickListener() {
			
			@Override
			public void onBack(String pf) {
				
				TTHttpRequestControll.httpNote(getActivity(),taskId,status ,new HttpRequestUtil.HttpRonspontCallBack() {
					
					@Override
					public void callBack(HttpResponse result) {
						
						try {
							if (result.content.equals("1")) {
								toast("提交成功");
								if (status.equals("end")) {
									getData();
								}
							}else{
								toast("提交失败");
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
						
							
						
					}
				});
				
			}
		});
		dilog.showDialog();
	}
}
